/**
 * 定义吃豆人类
 */
export class PacMan{
    constructor(option={}){
        let {color="#ffa200",size = 40,mLevel=10,dom} = option
        this.x = size
        this.y = size
        this.color = color
        this.size = size
        this.mStatus = true //吃豆人在此时应该执行张嘴动作还是执行闭嘴动作 true为张嘴  false为闭嘴
        this.mLevel = mLevel //吃豆人在张嘴或者闭嘴时 一共分为几个等级
        this.cMLevel = 0 //记录吃豆人嘴巴当前张开的等级
        this.speed = 20 //吃豆人速度
        this.direction = "down" //记录吃豆人运动方向
        this.dom = dom
    }

    setMouth(isOpen){
        this.mStatus = isOpen
    }

    nextMouth(){
        if(this.mStatus){ //此时嘴巴应该张开
            this.cMLevel ++

            if(this.cMLevel >= this.mLevel){//此时嘴巴已经张到最大,应该执行闭嘴动作
                this.cMLevel = this.mLevel
                this.mStatus = false
            }
        }else{
            this.cMLevel --

            if(this.cMLevel <= 0){//此时嘴巴完全闭合,下一步应该执行张嘴动作
                this.cMLevel = 0
                this.mStatus = true
            }
        }
        
    }

    nextLocation(){
        switch(this.direction){
            case "right":
                this.x += this.speed 

                if(this.x >= this.dom.width - this.size){
                    this.direction = "left"
                }
                break
            case "left":
                this.x -= this.speed

                if(this.x <= this.size){
                    this.direction = "right"
                }
                break
            case "up":
                this.y -= this.speed

                if(this.y <= this.size){
                    this.direction = "down"
                }
                break
            case "down":
                this.y += this.speed
                if(this.y >= this.dom.height - this.size){
                    this.direction = "up"
                }
                break
        }
    }

    setDir(dir){
        this.direction = dir
    }
}